package lumis.portal.presentation.core;

import java.lang.reflect.Constructor;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.MarkupStream;

/**
 * Creates the component to be used for a {@link LayoutFileTag}.
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class DefaultLumisPortalComponentFactory implements ILayoutFileComponentFactory
{
	 /**
	  * Component to be used to render a HTML fragment. 
	  * @since 6.0.0
	  */
	 private Class<? extends HTMLFragmentComponent> componentClass;
	 
	 /**
	  * If {@link #setRenderBodyOnly(boolean)} should
	  * automatically be set in case of layout file tags are to be stripped.
	  */
	 private boolean autoRenderBodyOnly;
 
	 /**
	  * Constructor for the class.
	  * @param componentClass class that knows how to render the {@link LayoutFileTag}.
	  * @param autoRenderBodyOnly if {@link #setRenderBodyOnly(boolean)} should
	  * automatically be set in case of layout file tags are to be stripped.
	  * @since 6.0.0
	  */
	 public DefaultLumisPortalComponentFactory(Class<? extends HTMLFragmentComponent> componentClass, boolean autoRenderBodyOnly) 
	 {
		 this.componentClass = componentClass;
		 this.autoRenderBodyOnly = autoRenderBodyOnly;
		 
	 }
	 
	 /**
	  * Constructor for the class.
	  * @param componentClass class that knows how to render the {@link LayoutFileTag}.
	  * @since 6.0.0
	  */
	 public DefaultLumisPortalComponentFactory(Class<? extends HTMLFragmentComponent> componentClass) 
	 {
		 this.componentClass = componentClass;
		 this.autoRenderBodyOnly = false;
		 
	 }
 
	/**
	 * Creates a component for the specified tag.
	 * @param container the tag's container.
	 * @param markupStream the markup stream.
	 * @param tag the tag to be rendered.
	 * @return the component.
	 * @since 6.0.0
	 */
	 public Component createComponent(MarkupContainer container, MarkupStream markupStream, LayoutFileTag tag)
	 {
		 HTMLFragmentComponent fragmentComponent;
		 Constructor<? extends HTMLFragmentComponent> declaredConstructor = null;
		  try 
		  {
			  declaredConstructor = componentClass.getDeclaredConstructor(String.class, boolean.class);
			  fragmentComponent = declaredConstructor.newInstance(tag.getId(), autoRenderBodyOnly);
		  } 
		  catch (NoSuchMethodException e) 
		  {
			  try
			  {
				  declaredConstructor = componentClass.getDeclaredConstructor(String.class);
				  fragmentComponent = declaredConstructor.newInstance(tag.getId());				  
			  }
			  catch (Exception e1) 
			  {
				  fragmentComponent = null;
				  throw new RuntimeException(e1);
			}
		  }
		  catch (Exception e) 
		  {
			  if(!(e instanceof RuntimeException))
			  {
				  fragmentComponent = null;
				  throw new RuntimeException(e);
			  }
			  else
			  {
				  throw (RuntimeException)e;
			  }
			  
		  }
	
		 return fragmentComponent;
	 }
}